<template>
  <el-form
    :model="form"
    label-width="130px"
    :rules="rules"
    ref="form"
    style="margin-top: -12px"
  >
    <el-form-item label="销量设置" class="one-label">
      <!-- 虚拟销量 -->
      <el-form-item label="虚拟销量：" label-width="170px" prop="unrealSalesNum" v-if="fromListPage == 30">
        <el-input v-model="form.unrealSalesNum" class="long-input-2" max="8"></el-input>
        <span class="tips">当前实际销量{{ form.salesNum }}</span>
        <div  class="tips">小程序上显示为虚拟+实际销量</div>
      </el-form-item>
      <!-- 虚拟上架人数 -->
      <el-form-item label="虚拟上架人数：" label-width="170px" prop="virtualPutawayNum" v-if="fromListPage == 30">
        <el-input v-model="form.virtualPutawayNum" class="long-input-2" max="8"></el-input>
        <span class="tips">当前实际上架人数{{ form.putawayNum }}</span>
        <div  class="tips">小程序上显示为虚拟+实际上架人数，在主播小程序为网红在卖人数</div>
      </el-form-item>
      <!-- 样品限购数 -->
      <el-form-item label="样品限购数：" label-width="170px" v-if="fromListPage == 30 && enterpriseConfig.anchorSelectionAuthority == '1'">
        <el-input v-model="form.specimenLimitNum" class="long-input-2" max="8"></el-input>
        <div class="tips">在主播小程序上购买样品的数量，填空或填0则为不限数量</div>
      </el-form-item>
    </el-form-item>
  </el-form>
</template>


<script>
import { listSupplier } from '@/api/user/supplier'
export default {
  props: {
    dict: { type: Object },
    fromListPage: { type: Number, default: 30 },
    goodsId: { type: String, default: '' }
  },
  data() {
    return {
      templateAreas: [],
      supplierType: '1',
      thirdGoodsId: '2',
      updataFlag: true,
      form: {
        supplierId: '',
        templateFlag: '1',
        templateId: '',
        templateName: '',
        // supplierPostFee: '0',
        supplyPrice: '0',
        skuFlag: '0',
        // 虚拟销量
        unrealSalesNum: '0',
        // 实际销量
        salesNum: '0',
        stock: '',
        stockWarn: '',
        // 实际上架人数
        putawayNum: 0,
        // 虚拟上架人数
        virtualPutawayNum: 0,
        // 样品限购数
        specimenLimitNum: 0
      },
      // 企业系统配置
      enterpriseConfig: {
        wholesaleAuthority: 0
      },
      flagBoolean: false,
      skuFlag: 0,
      stockBoolean: false,
      supplierList: [],
      rules: {
        // supplierPostFee: [
        //   { max: 14, message: '邮费最多输入14 个字符' },
        //   { required: true, message: '请填写运费', trigger: 'blur' },
        //   { pattern: /(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,2}$)/, message: '请正确输入邮费' }
        // ],
        unrealSalesNum: [
          { max: 8, message: '已经出售库存最多8个字节' },
          { required: false },
          { pattern: /^[0-9]*[0-9][0-9]*$/, message: '请正确输入已经出售库存' }
        ],
        stock: [
          { max: 8, message: '库存最多8个字节' },
          { required: true, message: '请填写商品库存', trigger: 'blur' },
          { pattern: /^[0-9]*[0-9]*$/, message: '请填写正确商品库存' }
        ],
        stockWarn: [
          { max: 8, message: '库存最多8个字节' },
          { required: false },
          { pattern: /^[0-9]*[1-9][0-9]*$/, message: '请填写正确库存预警' }
        ],
        supplierId: [{ required: true, message: '请选择来源', trigger: 'blur' }]
      }
    }
  },
  // 初始化页面
  created() {
    this.initEnterpriseConfig()
    this.listSupplier()
  },
  mounted() {
    console.log('goodsId', this.goodsId)
  },
  beforeDestroy() {
    this.$bus.$off('goods.edit.data')
  },
  methods: {
    // 初始化企业系统配置
    initEnterpriseConfig() {
      // 监听info页面的enterpriseConfig信息
      const that = this
      this.$bus.$on('info.enterpriseConfig', function(data) {
        that.enterpriseConfig = data
        if (data.warehouseAuthority === 1 && data.warehouseDeliverFlag === 1) {
          // 开启仓库库存，不允许编辑
          that.stockBoolean = true
          that.form.stock = '0'
        }
      })
    },
    initGoodsEdit() {
      const that = this
      this.$bus.$on('goods.edit.data', function(data) {
        if (data.goodsId && data.skuFlag * 1 === 1) {
          // 单规格可以改为多规格，反之则不行
          that.flagBoolean = true
        }
        const skuFlag = that.$utils.isExist(data.skuFlag)
          ? data.skuFlag + ''
          : '0'
        that.form.skuFlag = skuFlag
        that.form.unrealSalesNum = that.$utils.isExist(data.unrealSalesNum)
          ? data.unrealSalesNum + ''
          : '0'
        // 实际销量
        that.form.salesNum = that.$utils.isExist(data.salesNum) ? data.salesNum + '' : '0'
        // 虚拟上架人数
        that.form.virtualPutawayNum = that.$utils.isExist(data.virtualPutawayNum) ? data.virtualPutawayNum + '' : '0'
        // 实际上架人数
        that.form.putawayNum = that.$utils.isExist(data.putawayNum) ? data.putawayNum + '' : '0'
        // 样品限购数
        that.form.specimenLimitNum = that.$utils.isExist(data.specimenLimitNum) ? data.specimenLimitNum + '' : '0'
        // that.form.supplierPostFee = that.$utils.isExist(data.supplierPostFee) ? data.supplierPostFee : '0'
        that.form.stock = that.$utils.isExist(data.stock) ? data.stock + '' : ''
        that.form.stockWarn = that.$utils.isExist(data.stockWarn)
          ? data.stockWarn + ''
          : ''
        that.form.supplierId = that.$utils.isExist(data.supplierId)
          ? data.supplierId.toString()
          : ''
        that.form.thirdGoodsId = that.$utils.isExist(data.thirdGoodsId)
          ? data.thirdGoodsId.toString()
          : ''
        that.form.goodsId = that.$utils.isExist(data.goodsId)
          ? data.goodsId.toString()
          : ''
        if (data.goodsSource === 0) {
          that.updataFlag = true
        } else {
          that.updataFlag = false
        }
        that.form.goodsSource = data.goodsSource
        let stockBoolean
        if ((that.form.supplierId || data.thirdGoodsId) && that.form.goodsId) {
          stockBoolean = true
        } else {
          stockBoolean = false
        }
        if (that.enterpriseConfig.warehouseAuthority === 1 && that.enterpriseConfig.warehouseDeliverFlag === 1) {
          stockBoolean = true
        }
        that.$bus.$emit('goods_supplier.stockBoolean', stockBoolean)
        that.stockBoolean = stockBoolean
      })
    },
    listSupplier() {
      listSupplier({ enterpriseId: this.getEnterpriseId() }).then((response) => {
        if (response.isError) return
        this.supplierList = response.data
        this.initGoodsEdit()
        this.$emit('emitGoodsData')
      })
    },
    supplierChange() {
      let stockBoolean = true
      if (this.form.supplierId && this.form.goodsId) {
        stockBoolean = true
      } else {
        stockBoolean = false
      }
      if (this.enterpriseConfig.warehouseAuthority === 1 && this.enterpriseConfig.warehouseDeliverFlag === 1) {
        stockBoolean = true
      }
      this.stockBoolean = stockBoolean
      this.$bus.$emit('goods_supplier.stockBoolean', stockBoolean)
    },
    checkForm() {
      // 需要校验的集合
      const checkArrays = []
      // checkArrays.push('supplierPostFee')
      checkArrays.push('unrealSalesNum')
      const skuFlag = this.form.skuFlag
      if (skuFlag === '0') {
        checkArrays.push('sellPrice')
        checkArrays.push('stock')
      }
      let ret = true
      let message = ''
      this.$refs.form.validateField(checkArrays, (valid) => {
        if (valid !== '') {
          message = valid
          ret = false
          return
        }
      })
      if (!ret) {
        this.$message({
          type: 'error',
          message: message
        })
        return false
      }

      this.$refs['form'].validate((valid) => {
        if (valid) {
          ret = true
        }
      })
      return ret
    }
  }
}
</script>
<style lang="less" scoped>
.span-bar {
  padding: 0 50px;
}
.remove-btn {
  padding: 7px 6px !important;
}
.long-input {
  width: 80%;
}
.long-input-2 {
  width: 55%;
  margin-right: 5%;
}
.long-input-3 {
  width: 55%;
}
.short-input {
  width: 20%;
}
.short-input-2 {
  width: 25%;
}
.default-input {
  width: 50%;
}
.tips {
  color: gray;
  font-size: 12px;
  line-height: 1.2;
  padding-top: 5px;
}
.picture {
  width: 130px;
  height: 130px;
  display: block;
}
.pic-bar {
  position: relative;
  .close-icon {
    position: absolute;
    right: -30px;
    top: -11px;
    width: 31px;
    height: 40px;
    padding: 0 7px;
    cursor: pointer;
  }
  span {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100%;
    display: block;
    color: #fff;
    text-align: center;
    padding: 3px 0;
    background: rgba(0, 0, 0, 0.5);
    cursor: pointer;
  }
}
.pic-add {
  background: #fff;
  border: #d3d3d3 1px dashed;
  border-radius: 5px;
  padding: 0 45px;
}
</style>
